<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Implicit construction - Boost.Outcome documentation</title>
<link rel="stylesheet" href="../../css/boost.css" type="text/css">
<meta name="generator" content="Hugo 0.52 with Boostdoc theme">
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>

<link rel="icon" href="../../images/favicon.ico" type="image/ico"/>
<body><div class="spirit-nav">
<a accesskey="p" href="../../experimental/worked-example.html"><img src="../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../experimental/worked-example.html"><img src="../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../experimental/worked-example-long.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content">
  <div class="titlepage"><div><div><h1 style="clear: both">Implicit construction</h1></div></div></div>
  <p>The preceding code had the compiler stamp out a custom status code domain
for a user supplied <code>enum</code>. You now get the following types:</p>

<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="c1">// This is the status code generated for your custom enum type. It will implicitly construct from
</span><span class="c1">// values of enum custom_failure.
</span><span class="c1"></span><span class="k">using</span> <span class="n">custom_failure_code</span> <span class="o">=</span> <span class="n">BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE</span><span class="o">::</span><span class="n">quick_status_code_from_enum_code</span><span class="o">&lt;</span><span class="n">custom_failure</span><span class="o">&gt;</span><span class="p">;</span>

<span class="k">namespace</span> <span class="n">outcome_e</span> <span class="o">=</span> <span class="n">BOOST_OUTCOME_V2_NAMESPACE</span><span class="o">::</span><span class="n">experimental</span><span class="p">;</span>

<span class="c1">// You don&#39;t usually need to use the status code type explicitly, because this &#34;just works&#34;:
</span><span class="c1"></span><span class="n">outcome_e</span><span class="o">::</span><span class="n">status_result</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">positive_only</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">)</span>
<span class="p">{</span>
  <span class="k">if</span><span class="p">(</span><span class="n">x</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)</span>
  <span class="p">{</span>
    <span class="c1">// Outcome&#39;s result sees that status_code will implicitly construct from this enum,
</span><span class="c1"></span>    <span class="c1">// and it returns an errored result
</span><span class="c1"></span>    <span class="k">return</span> <span class="n">custom_failure</span><span class="o">::</span><span class="n">bad_argument</span><span class="p">;</span>
  <span class="p">}</span>
  <span class="k">return</span> <span class="n">x</span><span class="p">;</span>
<span class="p">}</span>

<span class="c1">// Semantic comparisons work
</span><span class="c1"></span><span class="kt">bool</span> <span class="n">test</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">)</span>
<span class="p">{</span>
  <span class="k">if</span><span class="p">(</span><span class="k">auto</span> <span class="n">r</span> <span class="o">=</span> <span class="n">positive_only</span><span class="p">(</span><span class="n">x</span><span class="p">);</span> <span class="o">!</span><span class="n">r</span><span class="p">)</span>
  <span class="p">{</span>
    <span class="k">if</span><span class="p">(</span><span class="n">r</span><span class="p">.</span><span class="n">error</span><span class="p">()</span> <span class="o">==</span> <span class="n">outcome_e</span><span class="o">::</span><span class="n">errc</span><span class="o">::</span><span class="n">invalid_argument</span><span class="p">)</span>
    <span class="p">{</span>
      <span class="n">std</span><span class="o">::</span><span class="n">cerr</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;Positive numbers only!&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
      <span class="k">return</span> <span class="nb">false</span><span class="p">;</span>
    <span class="p">}</span>
  <span class="p">}</span>
  <span class="k">return</span> <span class="nb">true</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/quick_status_code_from_enum.cpp#L73" class="code-snippet-url" target="_blank">View this code on Github</a></div>


<p>As you can see, this is less work than <a href="../../motivation/plug_error_code.html">plugging your custom enum
into <code>std::error_code</code></a>.
It also has C compatibility, and generates better codegen.</p>


        </div><p><small>Last revised: July 16, 2024 at 21:33:35 &#43;0100</small></p>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../experimental/worked-example.html"><img src="../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../experimental/worked-example.html"><img src="../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../experimental/worked-example-long.html"><img src="../../images/next.png" alt="Next"></a></div></body>
</html>
